package smcms.service.resources.Service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import smcms.commons.api.entity.result.CommonsResult;
import smcms.service.resources.FeignService.OrderFeignService;
import smcms.service.resources.FeignService.ProductFeignService;
import smcms.service.resources.FeignService.SsoFeignService;
import smcms.service.resources.FeignService.StoreFeignService;
import smcms.service.resources.Service.PermissionService;
import smcms.service.resources.Service.ResourcesService;
import smcms.service.resources.entity.OwnPermission;
import smcms.service.resources.entity.Permission;
import smcms.service.resources.entity.Permissions;
import smcms.service.resources.mapper.ResourcesMapper;
import smcms.service.resources.utils.GetCurrentService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class ResourcesServiceImpl implements ResourcesService {
    @Autowired
    private StoreFeignService storeFeignService;
    @Autowired
    private GetCurrentService getCurrentService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private SsoFeignService ssoFeignService;
    @Autowired
    private ProductFeignService productFeignService;
    @Autowired
    private ResourcesMapper resourcesMapper;
    @Autowired
    private OrderFeignService orderFeignService;
    @Autowired
    private CommonsResult commonsResult;
    @Override
    public List<Permissions> getCurrentPermission() {
        //通过nacos获取在线服务名称
        List<String> nacosServiceList = getCurrentService.getNacosService();
        List<Permissions> permissionsList = new ArrayList<>();
        //对服务名列表进行遍历
        nacosServiceList.forEach(nacosService -> {
            switch (nacosService){
                //如果是smcms-service-cloudstore
                case "smcms-service-cloudstore" :
                    //通过openfeign获取该服务函数名
                    CommonsResult storePermissions = storeFeignService.getStorePermissions();
                    List<Permission> storePermissionsData = (List<Permission>) storePermissions.getData();
                    Permissions permissions = new Permissions();
                    permissions.setServiceName(nacosService);
                    permissions.setPermission(storePermissionsData);
                    permissionsList.add(permissions);break;
                case "smcms-service-sso" :
                    CommonsResult ssoPermission = ssoFeignService.getSsoPermission();
                    List<Permission> ssoPermissionData = (List<Permission>) ssoPermission.getData();
                    Permissions ssoPermissions = new Permissions();
                    ssoPermissions.setPermission(ssoPermissionData);
                    ssoPermissions.setServiceName(nacosService);
                    permissionsList.add(ssoPermissions);
                    break;
                case "smcms-service-product" :
                    CommonsResult productPermission = productFeignService.getSsoPermission();
                    List<Permission> productPermissionData = (List<Permission>) productPermission.getData();
                    Permissions productPermissions = new Permissions();
                    productPermissions.setPermission(productPermissionData);
                    productPermissions.setServiceName(nacosService);
                    permissionsList.add(productPermissions);
                    break;
                case "smcms-service-order" :
                    CommonsResult orderPermission = orderFeignService.getOrderPermissions();
                    List<Permission> orderPermissionData = (List<Permission>) orderPermission.getData();
                    Permissions orderPermissions = new Permissions();
                    orderPermissions.setPermission(orderPermissionData);
                    orderPermissions.setServiceName(nacosService);
                    permissionsList.add(orderPermissions);
                    break;
                case "smcms-gateway":
                    break;
                case "smcms-scheduled":
                    break;
                default:
                    try {
                        List<Permission> permissionList = permissionService.getPermissions();
                        Permissions selfPermissions = new Permissions();
                        selfPermissions.setServiceName(nacosService);
                        selfPermissions.setPermission(permissionList);
                        permissionsList.add(selfPermissions);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
            }
        });

        return permissionsList;
    }

    @Override
    public CommonsResult syncPermission() {
        List<Permissions> currentPermission = getCurrentPermission();
        try {
            resourcesMapper.PermissionDelete();
            resourcesMapper.insertUserPermission(currentPermission);
            resourcesMapper.updatePermissionCall();
        }catch (Exception e) {
            commonsResult.failed("数据同步出现异常！");
        }
        return commonsResult.success("数据同步成功！");
    }

    @Override
    public List<Permissions> getOwnPermission(String username) {
        List<OwnPermission> ownPermission = resourcesMapper.getOwnPermission(username);
        List<Permissions> permissionsList = new ArrayList<>();
        Map<String, List<OwnPermission>> map = ownPermission.stream().
                collect(Collectors.groupingBy(OwnPermission::getService_name));
        map.keySet().forEach(key->{
            Permissions permissions = new Permissions();
            permissions.setServiceName(key);
            List<OwnPermission> ownPermissions = map.get(key);
            List<Permission> collect = ownPermissions.stream().map(m -> {
                Permission permission = new Permission();
                permission.setPermission(m.getPermission_name());
                return permission;
            }).collect(Collectors.toList());
            permissions.setPermission(collect);
            permissionsList.add(permissions);
        });
        return permissionsList;
    }

    @Override
    public CommonsResult grantePermission(Permission permission) {
        if (permission.getUsername().isEmpty()) {
            return commonsResult.failed("授权用户不能为空");
        }
        if (resourcesMapper.existPermission(permission)) {
            return commonsResult.failed("用户已有该权限无需赋值");
        }
        resourcesMapper.grantePermission(permission);
        return commonsResult.success("权限赋予成功");
    }
}
